package com.tongfusoft.shipping.service.impl.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;

import com.tongfusoft.base.data.tsm.TsmPriviledge;
import com.tongfusoft.base.data.tsm.TsmResources;
import com.tongfusoft.da.help.DBUtils;
import com.tongfusoft.utils.CryptoUtils;
import com.tongfusoft.utils.MapUtils;
import com.tongfusoft.web.core.data.PrivilegeData;
import com.tongfusoft.web.core.data.ResourceData;
import com.tongfusoft.web.core.data.User;
import com.tongfusoft.web.core.service.ISecurityService;

public class SecurityServiceImpl implements ISecurityService{

	@Override
	public Collection<GrantedAuthority> loadUserAuthoritiesByName(String userName) {
		List<Map<String,String>> list = DBUtils.executeSelect("/security#priviledgeByUserName",  MapUtils.factory().put("userName", userName).create());
		if(list != null && list.size() > 0){
			Collection<GrantedAuthority> result = new ArrayList<GrantedAuthority>(list.size());
			for(int i=0;i<list.size();i++){
				result.add(new GrantedAuthorityImpl(list.get(i).get("PRI_NAME")));
			}
			return result;
		}
		return Collections.EMPTY_LIST;
	}

	@Override
	public String getPwdByName(String userName) {
		List<Map<String,String>> list = DBUtils.executeSelect("/security#userPwdByName", MapUtils.factory().put("userName", userName).create());
		if(list != null && list.size() > 0){
			return list.get(0).get("USER_PWD");
		}
		return null;
	}

	@Override
	public User getUserInfo(String userName) {
		List<User> list = DBUtils.executeSelect("/security#userInfoByName", MapUtils.factory().put("userName", userName).create());
		if(list != null && list.size() == 1){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<PrivilegeData> getAllPrivilege() {
		List<TsmPriviledge> list = DBUtils.executeSelect("/security#AllPriviledge", TsmPriviledge.class, Integer.MAX_VALUE, 0, new HashMap());
		if(list != null && list.size() > 0){
			List<PrivilegeData> result = new ArrayList<PrivilegeData>();
			for(TsmPriviledge priviledge : list){
				result.add((PrivilegeData)priviledge);
			}
			return result;
		}else{
			return Collections.EMPTY_LIST;
		}
	}

	@Override
	public List<ResourceData> getResroucesByPrivilege(PrivilegeData privilege) {
		List<TsmResources> list = DBUtils.executeSelect("/security#resourcesByPriviledge", TsmResources.class, Integer.MAX_VALUE, 0, MapUtils.factory().put("priId", privilege.getPkValue()).create());
		if(list != null && list.size() > 0){
			List<ResourceData> result = new ArrayList<ResourceData>();
			for(TsmResources resources : list){
				result.add((ResourceData)resources);
			}
			return result;
		}else{
			return Collections.EMPTY_LIST;
		}
	}

	@Override
	public boolean validateUser(Map params) {
		if(StringUtils.equals(CryptoUtils.getMD5((String)params.get("j_password")), this.getPwdByName((String)params.get("j_username")))){
			return true;
		}
		return false;
	}

	@Override
	public Map<String, Object> getPredefineContent(User user, Map params) {
		Map<String,Object> map = new HashMap<String,Object>();
		return map;
	}

}
